package rpcserver

import (
	"net"
	"net/http"
	"net/rpc"

	"smartconn.cc/tosone/logstash"
)

var listen net.Listener

// App :an object export from app to service
type App int

// StartConn define
func StartConn() bool {
	logstash.Info("Init rpc server for app-end ...")
	//server := rpc.NewServer()
	app := new(App)
	rpc.Register(app)
	rpc.HandleHTTP()
	var err error
	for {
		listen, err = net.Listen("unix", "/tmp/nbc_app.rpc")
		if err != nil {
			logstash.Error(err.Error())
		} else {
			logstash.Info("Init rpc server for app-end successfully ...")
			break
		}
	}

	//server.Accept(l)

	go http.Serve(listen, nil)
	return true
}

// StopConn define
func StopConn() {
	logstash.Info("Stop rpc server for app-end ...")
	listen.Close()
}
